<template>
  <div class="talk">
    <input type="text" v-model="value" />
    <button @click="send">发送</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value: "",
    }
  },

  methods: {
    send() {
      // 建立一个WS连接
      var ws = new WebSocket("ws://localhost:8181")
      // 打开连接
      // 注意this的指向问题
      ws.onopen = (e) => {
        console.log("Connection to server opened")
        console.log(ws.readyState)
        ws.send(this.value)
      }

      ws.onmessage = function (message1) {
        console.log(message1.data)
      }

      ws.onclose = function () {
        console.log(`connection is closed`)
      }
    },
  },
}
</script>

<style lang="less" scoped></style>
